package com.xhu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xhu.pojo.Game;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface GameMapper extends BaseMapper<Game> {
    //查询所有游戏

    //通过user_id来查询拥有的游戏
    @Select("SELECT " +
            "g.* " +
            "FROM " +
            "game g " +
            "LEFT JOIN user_game_rel ugr ON ugr.game_id=g.game_id " +
            "LEFT JOIN `user` u ON u.user_id=ugr.user_id " +
            "WHERE " +
            "u.user_id=#{userId}")
    List<Game> gameListByUserId(@Param("userId")String  userId);

    //退货（删除user_game_rel中的关系）
    @Delete("DELETE FROM user_game_rel WHERE user_game_rel.user_id=#{userId} AND user_game_rel.game_id=#{gameId}")
    void deleteUserGameRel(@Param("userId")Integer userId,@Param("gameId")Integer gameId);

    //下架（删除user_game_rel中的关系和game中的游戏）

    //查询所有游戏
    @Select("SELECT * FROM game")
    List<Game> allGameList();

    @Insert("INSERT IGNORE INTO user_game_rel (user_id,game_id) " +
            "VALUES (#{userId},#{gameId})")
    void addGameByUser(@Param("userId")Integer userId,@Param("gameId")Integer gameId);
}
